package com.thomas.sdk.http;

import java.io.IOException;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.thomas.sdk.api.ApiConfiguration;
import com.thomas.sdk.manager.DebugUtility;

/**
 * 网络请求的封装类
 * 
 * @author thomas
 * 
 */
public class BHttpClient {
	public String get(String url, List<NameValuePair> headers)
			throws IOException {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);

		DebugUtility.showLog("REQUESTING " + url);

		if (headers != null) {
			for (int i = 0; i < headers.size(); i++) {
				httpget.addHeader(headers.get(i).getName(), headers.get(i)
						.getValue());
			}
		}

		if (ApiConfiguration.sku != null) {
			httpget.addHeader("x-beintoo-app-sku", ApiConfiguration.sku);
		}

		if (ApiConfiguration.buildId != null) {
			httpget.addHeader("x-beintoo-app-buildid", ApiConfiguration.buildId);
		}

		if (ApiConfiguration.version != null) {
			httpget.addHeader("x-beintoo-sdk-version", ApiConfiguration.version);
		}

		if (DebugUtility.isDebugEnable) {
			Header[] headerParams = httpget.getAllHeaders();
			DebugUtility.showLog("--------------------------");
			DebugUtility.showLog("HEADERS: " + new Gson().toJson(headerParams));
			DebugUtility.showLog("--------------------------");
		}

		// Execute HTTP Post Request
		HttpResponse response = httpclient.execute(httpget);
		String jsonResponse = null;
		if (response.getEntity() != null)
			jsonResponse = EntityUtils.toString(response.getEntity());
		else
			jsonResponse = "" + response.getStatusLine().getStatusCode();

		DebugUtility.showLog("RESPONSE " + jsonResponse);

		if (response.getStatusLine().getStatusCode() != 200) {
			throw new ApiException(jsonResponse, response.getStatusLine()
					.getStatusCode());
		} else {
			return jsonResponse;
		}
	}

	public String get(String url) throws IOException {
		return get(url, null);
	}

	public String post(String url, List<NameValuePair> params,
			StringEntity entity, List<NameValuePair> headers)
			throws IOException {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);

		DebugUtility.showLog("REQUESTING " + url);

		// add header params
		if (headers != null) {
			for (int i = 0; i < headers.size(); i++) {
				httppost.addHeader(headers.get(i).getName(), headers.get(i)
						.getValue());
			}
		}

		if (ApiConfiguration.sku != null) {
			httppost.addHeader("x-beintoo-app-sku", ApiConfiguration.sku);
		}

		if (ApiConfiguration.buildId != null) {
			httppost.addHeader("x-beintoo-app-buildid",
					ApiConfiguration.buildId);
		}

		if (ApiConfiguration.version != null) {
			httppost.addHeader("x-beintoo-sdk-version",
					ApiConfiguration.version);
		}

		httppost.addHeader("content-type", "application/json");

		if (DebugUtility.isDebugEnable) {
			Header[] headerParams = httppost.getAllHeaders();
			DebugUtility.showLog("--------------------------");
			DebugUtility.showLog("HEADERS: " + new Gson().toJson(headerParams));
			if (params != null)
				DebugUtility.showLog("FORM PARAMS: "
						+ new Gson().toJson(params));
			else if (entity != null)
				DebugUtility.showLog("STRING ENTITY PARAM: "
						+ EntityUtils.toString(entity));
			DebugUtility.showLog("--------------------------");
		}

		// add post params
		if (params != null)
			httppost.setEntity(new UrlEncodedFormEntity(params));
		else if (entity != null)
			httppost.setEntity(entity);

		// Execute HTTP Post Request
		HttpResponse response = httpclient.execute(httppost);
		String jsonResponse = EntityUtils.toString(response.getEntity(),
				"UTF-8");

		DebugUtility.showLog("RESPONSE " + jsonResponse + " "
				+ response.getStatusLine().getStatusCode());

		if (response.getStatusLine().getStatusCode() != 200) {
			throw new ApiException(jsonResponse, response.getStatusLine()
					.getStatusCode());
		} else {
			return jsonResponse;
		}
	}

	public String post(String url, List<NameValuePair> params)
			throws IOException {
		return post(url, params, null, null);
	}

	public String post(String url, List<NameValuePair> params,
			List<NameValuePair> headers) throws IOException {
		return post(url, params, null, headers);
	}

}
